// V-backend/src/models/Cart.js
module.exports = (sequelize, DataTypes) => {
  const Cart = sequelize.define('Cart', {
    cart_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      field: 'cart_id'
    },
    audience_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'audience_id'
    },
    product_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'product_id'
    },
    quantity: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      field: 'quantity'
    },
    created_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    },
    updated_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      field: 'updated_at'
    }
  }, {
    tableName: 'v_cart',
    timestamps: false,
    hooks: {
      beforeUpdate: (cart) => {
        cart.updated_at = new Date();
      }
    }
  });

  Cart.associate = function(models) {
    Cart.belongsTo(models.Audience, {
      foreignKey: 'audience_id',
      as: 'audience'
    });
    
    Cart.belongsTo(models.Product, {
      foreignKey: 'product_id',
      as: 'product'
    });
  };

  return Cart;
};